home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLR / glrduck / glrduckMainWindow.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  9.4 KB  |  353 lines

  1.  
  2. //////////////////////////////////////////////////////////////
  3. //
  4. // Source file for glrduckMainWindow
  5. //
  6. //    This class is a subclass of VkWindow
  7. //
  8. //
  9. // Normally, very little in this file should need to be changed.
  10. // Create/add/modify menus using RapidApp.
  11. //
  12. // Try to restrict any changes to the bodies of functions
  13. // corresponding to menu items, the constructor and destructor.
  14. //
  15. // Restrict changes to those sections between
  16. // the "//--- Start/End editable code block" markers
  17. //
  18. // Doing so will allow you to make changes using RapidApp
  19. // without losing any changes you may have made manually
  20. //
  21. // Avoid gratuitous reformatting and other changes that might
  22. // make it difficult to integrate changes made using RapidApp
  23. //////////////////////////////////////////////////////////////
  24. #include "glrduckMainWindow.h"
  25.  
  26. #include <Vk/VkApp.h>
  27. #include <Vk/VkFileSelectionDialog.h>
  28. #include <Vk/VkSubMenu.h>
  29. #include <Vk/VkRadioSubMenu.h>
  30. #include <Vk/VkMenuItem.h>
  31. #include <Vk/VkMenuBar.h>
  32. #include <Vk/VkResource.h>
  33.  
  34.  
  35. // Externally defined classes referenced by this class: 
  36.  
  37. //---- Start editable code block: headers and declarations
  38. #include <Inventor/nodes/SoSeparator.h>
  39. #include <Inventor/actions/SoGLRenderAction.h>
  40. #include <Inventor/nodes/SoCylinder.h>
  41. #include <Inventor/nodes/SoDirectionalLight.h>
  42. #include <Inventor/nodes/SoEventCallback.h>
  43. #include <Inventor/nodes/SoMaterial.h>
  44. #include <Inventor/nodes/SoPerspectiveCamera.h>
  45. #include <Inventor/nodes/SoRotationXYZ.h>
  46. #include <Inventor/nodes/SoTransform.h>
  47. #include <Inventor/nodes/SoTranslation.h>
  48. #include <Inventor/nodes/SoComplexity.h>
  49. #include "SoXtGLRRenderArea.h"
  50. //---- End editable code block: headers and declarations
  51.  
  52.  
  53.  
  54. // These are default's resources for widgets in objects of this class
  55. // All resources will be prepended by *<name> at instantiation,
  56. // where <name> is the name of the specific instance, as well as the
  57. // name of the baseWidget. These are only defaults, and may be overriden
  58. // in a resource file by providing a more specific resource name
  59.  
  60. String  glrduckMainWindow::_defaultglrduckMainWindowResources[] = {
  61.   "*exitButton.accelerator:  Ctrl<Key>Q",
  62.   "*exitButton.acceleratorText:  Ctrl+Q",
  63.   "*exitButton.labelString:  Exit",
  64.   "*exitButton.mnemonic:  x",
  65.   "*filePane.labelString:  File",
  66.   "*filePane.mnemonic:  F",
  67.   "*helpPane.labelString:  Help",
  68.   "*helpPane.mnemonic:  H",
  69.   //---- Start editable code block: glrduckMainWindow Default Resources
  70.  
  71.  
  72.   //---- End editable code block: glrduckMainWindow Default Resources
  73.  
  74.   (char*)NULL
  75. };
  76.  
  77.  
  78. //---- Class declaration
  79.  
  80. glrduckMainWindow::glrduckMainWindow ( const char *name,
  81.                      ArgList args,
  82.                      Cardinal argCount) : 
  83.   VkWindow ( name, args, argCount ), _moving(FALSE)
  84. {
  85.   int attrs[] = {GLR_RGBA, GLR_RED_SIZE, 1, GLR_GREEN_SIZE, 1, 
  86.          GLR_BLUE_SIZE, 1, GLR_DEPTH_SIZE, 16, 
  87.          GLR_SAMPLES_SGIS, 4, 0};
  88.  
  89.   // Load any class-default resources for this object
  90.  
  91.   setDefaultResources ( baseWidget(), _defaultglrduckMainWindowResources  );
  92.  
  93.  
  94.   // Create the view component contained by this window
  95.  
  96.   _render= new SoXtGLRRenderArea (attrs, mainWindowWidget(), "render");
  97.  
  98.  
  99.   XtVaSetValues ( _render->getWidget(),
  100.           XmNwidth, 500, 
  101.           XmNheight, 500, 
  102.           (XtPointer) NULL );
  103.  
  104.   // Add the component as the main view
  105.   // Inventor components are not really VkComponents,
  106.   // so we have to add them by their widgets
  107.  
  108.   addView ( _render->getWidget() );
  109.  
  110.   // Create the panes of this window's menubar. The menubar itself
  111.   // is created automatically by ViewKit
  112.  
  113.  
  114.   // The filePane menu pane
  115.  
  116.   _filePane =  addMenuPane ( "filePane" );
  117.  
  118.   _exitButton =  _filePane->addAction ( "exitButton", 
  119.                                         &glrduckMainWindow::quitCallback, 
  120.                                         (XtPointer) this );
  121.  
  122.  
  123.   //---- Start editable code block: glrduckMainWindow constructor
  124.   _root = new SoSeparator;
  125.   _root->ref();
  126.  
  127.   _complexity     = new SoComplexity;
  128.   _complexity->value = 0.4;
  129.   _root->addChild(_complexity);
  130.  
  131.   // Add a camera and light
  132.   SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
  133.   myCamera->position.setValue(0., -4., 8.0);
  134.   myCamera->heightAngle = M_PI/2.5; 
  135.   myCamera->nearDistance = 1.0;
  136.   myCamera->farDistance = 15.0;
  137.  
  138.   _root->addChild(myCamera);
  139.   _root->addChild(new SoDirectionalLight);
  140.  
  141.   // Rotate scene slightly to get better view
  142.   SoRotationXYZ *globalRotXYZ = new SoRotationXYZ;
  143.   globalRotXYZ->axis = SoRotationXYZ::X;
  144.   globalRotXYZ->angle = M_PI/9;
  145.   _root->addChild(globalRotXYZ);
  146.  
  147.   // Pond group
  148.   SoSeparator *pond = new SoSeparator; 
  149.   _root->addChild(pond);
  150.  
  151.   SoMaterial *cylMaterial = new SoMaterial;
  152.   cylMaterial->diffuseColor.setValue(0., 0.3, 0.8);
  153.   pond->addChild(cylMaterial);
  154.  
  155.   SoTranslation *cylTranslation = new SoTranslation;
  156.   cylTranslation->translation.setValue(0., -6.725, 0.);
  157.   pond->addChild(cylTranslation);
  158.  
  159.   SoCylinder *myCylinder = new SoCylinder;
  160.   myCylinder->radius.setValue(4.0);
  161.   myCylinder->height.setValue(0.5);
  162.   pond->addChild(myCylinder);
  163.  
  164.   // Duck group
  165.   SoSeparator *duck = new SoSeparator;
  166.   _root->addChild(duck);
  167.  
  168.   // Read the duck object from a file and add to the group
  169.   SoInput myInput;
  170.   if (!myInput.openFile("duck.iv")) {
  171.     if (!myInput.openFile("/usr/share/src/Inventor/examples/data/duck.iv")) {
  172.       exit(1);
  173.     }
  174.   }
  175.   SoSeparator *duckObject = SoDB::readAll(&myInput);
  176.   if (duckObject == NULL) exit(1);
  177.  
  178.   // Set up the duck transformations
  179.   _duckRotXYZ = new SoRotationXYZ;
  180.   duck->addChild(_duckRotXYZ);
  181.   _duckRotXYZ->angle = _angle = 0.0;
  182.   _duckRotXYZ->axis = SoRotationXYZ::Y;  // rotate about Y axis
  183.  
  184.   SoTransform *initialTransform = new SoTransform;
  185.   initialTransform->translation.setValue(0., 0., 3.);
  186.   initialTransform->scaleFactor.setValue(6., 6., 6.);
  187.   duck->addChild(initialTransform);
  188.  
  189.   duck->addChild(duckObject);
  190.  
  191.   _render->setSceneGraph(_root);
  192.   _render->setEventCallback(myEventCB, this);
  193.   //---- End editable code block: glrduckMainWindow constructor
  194.  
  195.  
  196. }    // End Constructor
  197.  
  198.  
  199. glrduckMainWindow::~glrduckMainWindow()
  200. {
  201.   delete _render;
  202.   //---- Start editable code block: glrduckMainWindow destructor
  203.  
  204.  
  205.   //---- End editable code block: glrduckMainWindow destructor
  206. }    // End destructor
  207.  
  208.  
  209. const char *glrduckMainWindow::className()
  210. {
  211.   return ("glrduckMainWindow");
  212. }    // End className()
  213.  
  214.  
  215. Boolean glrduckMainWindow::okToQuit()
  216. {
  217.   //---- Start editable code block: glrduckMainWindow okToQuit
  218.  
  219.  
  220.  
  221.   // This member function is called when the user quits by calling
  222.   // theApplication->terminate() or uses the window manager close protocol
  223.   // This function can abort the operation by returning FALSE, or do some.
  224.   // cleanup before returning TRUE. The actual decision is normally passed on
  225.   // to the view object
  226.  
  227.  
  228.     // The view object is an Inventor component, which does
  229.     // not currently support the okToQuit protocol
  230.  
  231.   return ( TRUE );
  232.   //---- End editable code block: glrduckMainWindow okToQuit
  233. }    // End okToQuit()
  234.  
  235.  
  236.  
  237. /////////////////////////////////////////////////////////////// 
  238. // The following functions are static member functions used to 
  239. // interface with Motif.
  240. /////////////////////////////////// 
  241.  
  242. void glrduckMainWindow::quitCallback ( Widget    w,
  243.                                         XtPointer clientData,
  244.                                         XtPointer callData ) 
  245.   glrduckMainWindow* obj = ( glrduckMainWindow * ) clientData;
  246.   obj->quit ( w, callData );
  247. }
  248.  
  249.  
  250.  
  251.  
  252.  
  253. /////////////////////////////////////////////////////////////// 
  254. // The following functions are called from callbacks 
  255. /////////////////////////////////// 
  256.  
  257. void glrduckMainWindow::quit ( Widget, XtPointer ) 
  258. {
  259.   // Exit via quitYourself() to allow the application
  260.   // to go through its normal shutdown routines, checking with
  261.   // all windows, views, and so on.
  262.  
  263.   theApplication->quitYourself();
  264.  
  265. }    // End glrduckMainWindow::quit()
  266.  
  267.  
  268.  
  269.  
  270. //---- Start editable code block: End of generated code
  271.  
  272. SbBool glrduckMainWindow::myEventCB(void *userData, XAnyEvent *event)
  273. {
  274.   SbBool done= NULL;;
  275.  
  276.   if (userData)
  277.     done = ((glrduckMainWindow*)userData)->myRealEventCB(event);
  278.  
  279.   return done;
  280. }
  281.  
  282. SbBool 
  283. glrduckMainWindow::myRealEventCB(XAnyEvent *anyevent)
  284. {
  285.   XButtonEvent *myButtonEvent;
  286.   XMotionEvent *myMotionEvent;
  287.   SbVec3f       vec;
  288.   SbBool        handled = FALSE;
  289.  
  290.   switch (anyevent->type) {   
  291.   case ButtonPress:
  292.     myButtonEvent = (XButtonEvent *) anyevent;
  293.     if (myButtonEvent->button == Button1) {
  294.       motionStart(myButtonEvent->x, myButtonEvent->y);
  295.       handled = TRUE;
  296.     }
  297.     break;      
  298.   case ButtonRelease:
  299.     myButtonEvent = (XButtonEvent *) anyevent;
  300.     if (myButtonEvent->button == Button1) {
  301.       if (_moving) {
  302.     motionStop();
  303.     handled = TRUE;
  304.       }
  305.     }
  306.     break;      
  307.   case MotionNotify:
  308.     myMotionEvent = (XMotionEvent *) anyevent;
  309.     if (myMotionEvent->state & Button1Mask) {  
  310.       motion(myMotionEvent->x, myMotionEvent->y);
  311.     }
  312.     break;      
  313.   default:
  314.     break;
  315.   }
  316.    
  317.   return handled;
  318. }
  319.  
  320. void glrduckMainWindow::motionStart(int x, int y)
  321. {
  322.   _complexity->value = 0.4;
  323.   _begin = x;
  324. }
  325.  
  326. void glrduckMainWindow::motion(int x, int)
  327. {
  328.   _moving = TRUE;
  329.   _angle = _angle + .01 * (x - _begin);
  330.   _begin = x;
  331.   _duckRotXYZ->angle = _angle;
  332.   _render->render(SoXtGLRRenderArea::LOW);
  333. }
  334.  
  335. void glrduckMainWindow::motionStop()
  336. {
  337.   extern VkApp             *theApplication;
  338.  
  339.   _moving = FALSE;
  340.   _complexity->value = 1.0;
  341.   theApplication->busy();
  342.   _render->render(SoXtGLRRenderArea::HIGH);
  343.   theApplication->notBusy();
  344.   _render->setAutoRedraw(FALSE);
  345.   _complexity->value = 0.4;
  346. }
  347.  
  348.  
  349. //---- End editable code block: End of generated code
  350.  
  351.  
  352.